package com.tjsinfo.tjsoa.useseal.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealBO;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealFileBO;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSeal;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSealFile;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class SealAddAction extends Action {
	private ITUseSealSealBO sealBo;
	private ITUseSealSealFileBO sealFileBo;
	
	public void setSealBo(ITUseSealSealBO sealBo) {
		this.sealBo = sealBo;
	}
	public void setSealFileBo(ITUseSealSealFileBO sealFileBo) {
		this.sealFileBo = sealFileBo;
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			Integer sealMark = Integer.parseInt(request.getParameter("sealMark"));
			String sealName = request.getParameter("sealName");
			String sealRea = request.getParameter("sealRea");
			String status = "审核中";
			String remarks = request.getParameter("remarks");
			Integer applyId = ToolKit.getSessionUser(request).getUser_id();
			String applyName = ToolKit.getSessionUser(request).getUsername();
			String deptId = request.getParameter("deptId");
			String officeId = request.getParameter("officeId");
			
			String linkurl = null;
			String message = null;
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			Long flowid = null;
			switch (sealMark) {
			case 0:
				flowid = test.creatProcessInstance("申请人所在部门部长审批",deptId,"useSealGen",null);
				test.setVariable("办公室主管审批",officeId, flowid);
				
				linkurl = "useSeal/gen/genList.jsp";
				message = "useSeal.genAdd.message";
				break;
			case 1:
				String dept1Id = request.getParameter("dept1Id");
				flowid = test.creatProcessInstance("申请人所在部门部长审批",deptId,"useSealImpName",null);
				test.setVariable("申请人所在部门主管领导审批",dept1Id, flowid);
				test.setVariable("办公室主管审批",officeId, flowid);
				
				linkurl = "useSeal/imp/impList.jsp";
				message = "useSeal.impAdd.message";
				break;
			case 2:
				String nameId = request.getParameter("nameId");
				flowid = test.creatProcessInstance("申请人所在部门部长审批",deptId,"useSealImpName",null);
				test.setVariable("申请人所在部门主管领导审批",nameId, flowid);
				test.setVariable("办公室主管审批",officeId, flowid);
				
				linkurl = "useSeal/name/nameList.jsp";
				message = "useSeal.nameAdd.message";
				break;				
			}
			
			TUseSealSeal tuss = new TUseSealSeal();
			tuss.setFlowid(Integer.parseInt(flowid.toString()));
			tuss.setDateTime(sdf.parse(sdf.format(date)));
			//tuss.setDeptId(deptId);
			//tuss.setDeptName(deptName);			
			tuss.setApplyId(applyId.toString());
			tuss.setApplyName(applyName);
			tuss.setSealName(sealName);
			tuss.setSealRea(sealRea);
			tuss.setStatus(status);
			tuss.setRemarks(remarks);
			tuss.setUserId(",");
			tuss.setMark(sealMark);
			String id = sealBo.saveId(tuss);
			
			MessageResources message1=getResources(request);
			String fromFilePath=message1.getMessage(request.getLocale(), "system.uploadFile.tempFilePath");
			String toFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/useseal/properties/systemResource.properties", this).getProperty("useSeal.fileurl");
			List<Files> fileList=DealString.fileCopyTofile(request, fromFilePath, toFilePath);	
			for (Iterator iterator = fileList.iterator(); iterator.hasNext();) {
				Files file = (Files) iterator.next();
				TUseSealSealFile tssf = new TUseSealSealFile();
				tssf.setSealId(id);
				tssf.setNewFileName(file.newFileName);
				tssf.setOldFileName(file.oldFileName);
				tssf.setToFilePath(file.toFilePath);
				sealFileBo.save(tssf);
			}			
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);				
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}


}
